//
//  YCPrintTableViewCell.m
//  lianganyuchu
//
//  Created by iMac on 17/3/25.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCPrintTableViewCell.h"
#define BtnHeight 50

@interface YCPrintTableViewCell ()
{
    UIView *m_backView;
}
@end
@implementation YCPrintTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.label1 setFont:[UIFont LightFontOfSize:14]];
    [self.label1 setTextColor:TEXT_LIGHT];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

-(void)bind
{
    CGFloat width=kMainScreen_Width-2*kDistance;
    CGFloat height=40+_deviceArray.count*50;
    [self.backView addShadowWith:CGSizeMake(width, height)];
    
    if (_deviceArray.count>0) {
        if (m_backView==nil) {
            m_backView=[[UIView alloc]init];
        }
        [[m_backView subviews]
         makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [m_backView setFrame:CGRectMake(0, 40, width, _deviceArray.count*BtnHeight)];
        
        for (int i=0; i<_deviceArray.count; i++) {
            CBPeripheral *peripheral = [self.deviceArray objectAtIndex:i];
            
            YCPrintButton *printBtn=[[YCPrintButton alloc]initWithFrame:CGRectMake(0, i*BtnHeight, width, BtnHeight)];
            [printBtn setDataWith:peripheral.name];
            if (peripheral.state==CBPeripheralStateConnected) {
                printBtn.selected=YES;
                [printBtn setSelected:YES];
            }else{
                printBtn.selected=NO;
                [printBtn setSelected:NO];
            }
            printBtn.tag=i;
            [printBtn addTarget:self action:@selector(printBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [m_backView addSubview:printBtn];
        }
    }else{
        [m_backView removeFromSuperview];
    }
    [self.backView addSubview:m_backView];
    
}
-(void)printBtnClicked:(UIButton *)button
{
    button.selected=!button.selected;
    CBPeripheral *peripheral =self.deviceArray[button.tag];
    if (peripheral.state==CBPeripheralStateConnected) {
        self.connectBlock(peripheral,NO);
    }else{
        self.connectBlock(peripheral,YES);
    }
}

@end





